package com.hspedu.houserent.view;

import com.hspedu.houserent.domain.House;
import com.hspedu.houserent.service.HouseService;
import com.hspedu.houserent.utils.Utility;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/1/25
 * @time 17:23
 */
public class HouseView {

    private boolean flag = true;
    private char key;
    private HouseService hs = new HouseService(1);

    public void listHouses() {
        System.out.println("--------------房屋列表---------------");
        House[] houses = hs.list();
        for (int i = 0; i < houses.length; i++) {
            if (houses[i] == null) {
                break;
            }
            System.out.println(houses[i]);
        }
    }

    public void delHouse() {
        boolean temp = true;
        boolean b = false;
        do {
            System.out.println("请选择待删除的房屋编号（-1退出）：");
            int id = Utility.readInt(0);
            if (id == -1) {
                System.out.println("放弃删除,成功退出");
                return;
            }
            System.out.println("确认是否删除（y/n），请小心选择");
            key = Utility.readChar();
            switch (key) {
                case 'y':
                    System.out.println("确认删除");
                    b = hs.delHouse(id);
                    temp = false;
                    break;
                case 'n':
                    System.out.println("取消删除");
                    temp = false;
                    break;
                default:
                    System.out.println("输入错误，请重新输入");
            }
        } while (temp);

        if (b) {
            System.out.println("删除成功");
        } else {
            System.out.println("没有对应的房屋，输入不正确\n删除失败");
        }

    }


    /**
     * 其实根据findHouse()这个方法稍微修改一下即可
     * HouseService中也不再需要写updateHouseById方法
     */
    public void updateHouse() {
        boolean bool = true;
        do {
            System.out.println("请选择待修改的房屋编号（-1退出）：");
            int id = Utility.readInt(0);
            if (id == -1) {
                System.out.println("放弃修改,成功退出");
                return;
            }
            for (int i = 0; i < hs.getHouseNum(); i++) {
                int index = 0;
                if (id == hs.getHouses()[i].getId()) {
                    index = i;
                    System.out.println("姓名:（" + hs.getHouses()[i].getHostName() + "）");
                    String name = Utility.readString(10, hs.getHouses()[i].getHostName());
                    System.out.println("电话：(" + hs.getHouses()[i].getPhoneNum() + ")");
                    String phoneNum = Utility.readString(13, hs.getHouses()[i].getPhoneNum());
                    System.out.println("地址：（" + hs.getHouses()[i].getAddress() + "）");
                    String address = Utility.readString(10, hs.getHouses()[i].getAddress());
                    System.out.println("租金:(" + hs.getHouses()[i].getRent() + ")");
                    int rent = Utility.readInt(hs.getHouses()[i].getRent());
                    System.out.println("状态：（" + hs.getHouses()[i].getState() + "）");
                    String state = Utility.readString(10, hs.getHouses()[i].getState());
                    hs.updateHouseById(name, phoneNum, address, rent, state, index);
                    System.out.println("--------------修改完成---------------");
                    bool = false;
                } else {
                    System.out.println("编号错误,请重新输入");
                }
            }
        } while (bool);
    }

    public void exit() {
        char c = Utility.readConfirmSelection();
        if (c == 'Y') {
            flag = false;
        }
    }

    public void addHouse() {
        System.out.println("--------------添加房屋---------------");
        System.out.print("姓名:");
        String hostName = Utility.readString(8);
        System.out.print("电话：");
        String phoneNum = Utility.readString(12);
        System.out.print("地址：");
        String address = Utility.readString(16);
        System.out.print("租金：");
        int rent = Utility.readInt(10);
        System.out.println("房屋当前状态:");
        String state = Utility.readString(3);
        boolean b = hs.addHouse(0, hostName, phoneNum, address, rent, state);
        if (b) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    public void findHouse() {
        System.out.println("--------------查找房屋---------------");
        System.out.println("请输入你要查找的id:");
        int id = Utility.readInt(0);
        House house = hs.getHouseById(id);
        if (house != null) {
            System.out.println(house);
        } else {
            System.out.println("没有找到相应的信息");
        }
    }

    public void mainMenu() {
        do {

            System.out.println("\n--------------房屋出租系统---------------");
            System.out.println("\t\t\t1 新 增 房 源");
            System.out.println("\t\t\t2 查 找 房 屋");
            System.out.println("\t\t\t3 删 除 房 屋");
            System.out.println("\t\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t\t5 房 屋 列 表");
            System.out.println("\t\t\t6 退\t出");
            System.out.println("请输入您的选择(1-6)：");
            key = Utility.readChar();
            switch (key) {
                case '1':
                    addHouse();
                    break;
                case '2':
                    findHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    updateHouse();
                    break;
                case '5':
                    listHouses();
                    break;
                case '6':
                    exit();
                    break;
                default:
                    System.out.println("输入非法，请重新输入");
                    break;
            }

        } while (flag);
    }
}
